package com.woniuxy.lifeorder.service.controller;


import com.woniuxy.life.core.api.CommonResult;
import com.woniuxy.life.core.api.OrderCode;
import com.woniuxy.life.core.api.ResultCode;
import com.woniuxy.lifeorder.service.entity.BusOrder;
import com.woniuxy.lifeorder.service.service.BusOrderService;
import com.woniuxy.lifeorder.service.vo.OrderInsertVO;
import com.woniuxy.lifeorder.service.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author 陈瑜洲，吴继明
 * @since 2020-09-24
 */
@RestController
@RequestMapping("/bus-order")
public class BusOrderController {
        @Autowired
        private BusOrderService busOrderService;

    /**
     * 订单查询--模糊--分页
     * @param current 当前页码
     * @param size     页大小
     * @param busOrder 传入对象，模糊查询
     * @return
     */
        @GetMapping("/list")
        public CommonResult<OrderVO> pageOrder(@RequestParam(value = "page",defaultValue = "1") Integer current, @RequestParam(value = "size",defaultValue = "1") Integer size, BusOrder busOrder){
            CommonResult<OrderVO> orderVOCommonResult=new CommonResult<>();
            try {
                OrderVO orderVO = busOrderService.order_query(current, size, busOrder);
                orderVOCommonResult.setData(orderVO);
                orderVOCommonResult.setCode(ResultCode.SUCCESS.getCode());
                orderVOCommonResult.setMessage(ResultCode.SUCCESS.getMessage());
            }catch (Exception e){
               return CommonResult.failed();
            }
            return orderVOCommonResult;
        }

    /**
     * 订单更新 并插入订单的更新时间
     * @param busOrder
     * @return
     */
    @PutMapping("/update")
        public CommonResult updateOrder(BusOrder busOrder){
            if (busOrder!=null){
                try {
                busOrder.setUpdateTime(LocalDateTime.now());
                boolean b = busOrderService.updateById(busOrder);
            }catch (Exception e){
                return CommonResult.failed(500L,"订单更新失败");
            }
                return CommonResult.success(busOrder);
            }else {
                return CommonResult.failed(500L,"参数异常");
            }

        }
    @PostMapping(value = "/order")
    public CommonResult insertOrder(@RequestBody OrderInsertVO insertVO){
//        System.out.println("1"+userId);
//        System.out.println("2"+outUserId);
//        System.out.println(goods);
        System.out.println(insertVO);
        try {
            //busOrderService.order_insert(userId,outUserId,orderTypeId,goods);
            busOrderService.order_insert(insertVO.getUserId(),insertVO.getOutUserId(),insertVO.getOrderTypeId(),insertVO.getGoods());
        }catch (Exception e){
            return CommonResult.failed(500L,"订单生成失败");
        }
        return CommonResult.success();
    }

    /**
     * 根据id主键 更改订单软删除项为1
   * @param orderId
     * @return
     */
    @DeleteMapping("/order")
    public CommonResult deleteOrder(Integer orderId){
        try {
            BusOrder busOrder=new BusOrder();
            busOrder.setOrderId(orderId);
            busOrder.setFlag(OrderCode.FALG_DELETE);
            busOrderService.updateById(busOrder);
        }catch (Exception e){
            CommonResult.failed(500L,"订单删除失败");
        }
        return CommonResult.success();
    }
}
